package com.zhuss.car.processor;

import java.util.Date;
import java.util.List;

import com.zhuss.car.processor.cache.CarCache;
import com.zhuss.enums.StartStopEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import com.zhuss.bean.bo.car.car.CreateCarBo;
import com.zhuss.car.db.dao.ext.CarDao;
import com.zhuss.car.db.entity.Car;
import com.zhuss.common.AbstractProcessor;
import com.zhuss.common.Processor;
import com.zhuss.db.entity.OpLog;
import com.zhuss.enums.LogActionEnum;
import com.zhuss.enums.MoudleEnum;
import com.zhuss.exception.DzcxException;
import com.zhuss.exception.ExceptionCode;
import com.zhuss.util.JsonUtil;


/**
 * @description: 车辆创建
 * @author: zhushanshuo
 * @create: Fri Dec 18 20:35:18 CST 2020
 **/
@Component("carCreate")
public class CarCreate extends AbstractProcessor<Object, CreateCarBo> implements Processor<Object, CreateCarBo> {

    @Autowired
    private CarDao carDao;

    @Autowired
	@Qualifier("carRead")
	private CarRead carRead;

	@Autowired
	@Qualifier("carCache")
	private CarCache carCache;


    /**
     * 校验
     */
    @Override
    public void check(CreateCarBo bo) throws DzcxException {
    	String plateNo = bo.getPlateNo();

    	if (bo.getRideType() == null || bo.getRideType()== 0) {
			throw new DzcxException(ExceptionCode.乘车类型为空);
		}

		if (bo.getType() == null || bo.getType()== 0) {
			throw new DzcxException(ExceptionCode.车辆类型为空);
		}

		if (bo.getPeopleQuantity() == null || bo.getType()== 0) {
			throw new DzcxException(ExceptionCode.核载数量为空);
		}

    	if (plateNo == null || "".equals(plateNo.trim())) {
    		throw new DzcxException(ExceptionCode.车辆plateNo为空);
    	}

		if (bo.getCompanyId() == null || bo.getCompanyId() <=0l) {
			throw new DzcxException(ExceptionCode.所属公司为空);
		}

    	List<Car> list = carRead.list(plateNo);
    	if (!list.isEmpty()) {
    		throw new DzcxException(ExceptionCode.车辆plateNo已存在);
    	}
    }

    /**
     * 操作方法
     */
    @Override
    public Object process(CreateCarBo param) {
    	Car record = new Car();
    	record.setPlateNo(param.getPlateNo());
    	record.setType(param.getType());
    	record.setCompanyId(param.getCompanyId());
    	record.setDutyUser(param.getDutyUser());
    	record.setDutyPhone(param.getDutyPhone());
    	record.setPeopleQuantity(param.getPeopleQuantity());
		record.setStatus(param.getStatus() == null ? StartStopEnum.启用.key() : param.getStatus());
    	record.setPic(param.getPic());
    	record.setSort(param.getSort());
		record.setRideType(param.getRideType());
    	record.setCreatorId(param.getConcurrentUserId());
    	record.setCreator(param.getConcurrentUserName());
    	record.setCreateTime(new Date());
    	carDao.create(record);
    	param.setTargetId(record.getCarId());
    	carCache.clear(record.getCarId());
        return null;
    }

    /**
     * 操作日志
     */
	@Override
	public Object log(CreateCarBo param) throws DzcxException {
		OpLog record = new OpLog();
    	record.setMoudle(MoudleEnum.车辆.key());
    	record.setAction(LogActionEnum.车辆创建.key());
    	record.setTargetId(param.getTargetId());
    	record.setTargetName(param.getPlateNo());
    	record.setContent("参数: " + JsonUtil.toJson(param));
    	record.setReason(param.getExceptionReason());

    	record.setCreatorId(param.getConcurrentUserId());
    	record.setCreator(param.getConcurrentUserName());
    	record.setCreateTime(new Date());
    	log(record);
    	return null;
	}
}
